/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.renren.common.entity.XlsUpgradeAccountEntity;
import io.renren.common.entity.XlsUserEntity;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.dto.RegisterDTO;
import io.renren.service.XlsUpgradeAccountService;
import io.renren.service.XlsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 注册接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "注册接口")
public class ApiRegisterController {
    @Autowired
    private XlsUserService userService;

    @PostMapping("register")
    @ApiOperation("注册")
    public Result register(/*@RequestBody*/ RegisterDTO dto) {
        Result result = new Result();
        XlsUserEntity xlsUserByMobile = userService.getByMobile(dto.getMobile());
        if (ObjectUtil.isNotEmpty(xlsUserByMobile)) {
            result.setMsg("注册失败，此用户的用户名或手机号已注册！");
            return result;
        }
        XlsUserEntity userByUsername = userService.getUserByUsername(dto.getUsername());
        if (ObjectUtil.isNotEmpty(userByUsername)) {
            result.setMsg("注册失败，此用户的用户名或手机号已注册！");
            return result;
        }
        //表单校验
        ValidatorUtils.validateEntity(dto);
        String inviterId = "";
        if (StrUtil.isNotEmpty(dto.getInviterPhone())) {
            //通过手机号获取邀请人
            XlsUserEntity xlsUserEntity = userService.getByMobile(dto.getInviterPhone());
            if (ObjectUtil.isNotEmpty(xlsUserEntity)) {
                inviterId = xlsUserEntity.getId() + "";
            }
        }
        try {
            XlsUserEntity user = XlsUserEntity.builder()
                    .mobile(dto.getMobile())
                    .username(dto.getUsername())
                    .password(DigestUtils.sha256Hex(dto.getPassword()))
                    .createTime(new Date())
                    .inviterId(inviterId)
                    .build();
            userService.registerUsers(user);
//            user.setCircleLevel(0);
//            userService.insertUser(user);
        } catch (Exception e) {
            result.error("注册失败");
            return result;
        }
        result.setMsg("注册成功！");
        return result;
    }

    @PostMapping("bregister")
    @ApiOperation("注册")
    public Result bregister() {
        Result result = new Result();
        for (int i = 1; i < 1001; i++) {
            XlsUserEntity user = XlsUserEntity.builder()
                    .username(i + "")
                    .password(DigestUtils.sha256Hex("123"))
                    .createTime(new Date())
                    .mobile(i + "")
                    .id(Long.valueOf(i))
                    .circleLevel(0)
                    .build();
            int inviterId = i / 7;
            user.setInviterId(inviterId + 1 + "");
            userService.insertUser(user);
        }

        result.setMsg("注册成功！");
        return result;
    }

}